package com.zf.rpc.hook;

import com.zf.rpc.util.NacosUtil;
import com.zf.rpc.factory.ThreadPoolFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ProjectName: RPC-zhaofeng
 * @Package: com.zf.rpc.hook
 * @ClassName: ShutdownHook
 * @Author: zf
 * @Description:
 * @Date: 2021/5/18 19:18
 * @Version: 1.0
 */
public class ShutdownHook {
    private static final Logger logger = LoggerFactory.getLogger(ShutdownHook.class);

    private static final ShutdownHook shutdownHook = new ShutdownHook();

    public static ShutdownHook getShutdownHook() {
        return shutdownHook;
    }

    public void addClearAllHook(){
        logger.info("关闭后将自动注销所有服务");

        Runtime.getRuntime().addShutdownHook(new Thread(() ->{
            NacosUtil.clearRegistry();
            ThreadPoolFactory.shutDownAll();
        }));
    }
}
